--守实利民
local shoushilimin = fk.CreateSkill {
    name = "mcdh_shoushilimin",
    tags = {Skill.Compulsory}
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

shoushilimin:addEffect(fk.EventPhaseEnd,{
    can_trigger = function(self, event, target, player, data)
        if not player:hasSkill(self) then return false end
        if target.phase ~= Player.Play then return false end
        local room = player.room
        local colorx, hasCard = 0, false
        room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
            for _, move in ipairs(e.data) do
                if move.toArea == Card.DiscardPile then
                    for _, info in ipairs(move.moveInfo) do
                        local card = Fk:getCardById(info.cardId)
                        if room:getCardArea(info.cardId) == Card.DiscardPile then
                            hasCard = true
                            colorx = colorx + (card.color == Card.Red and 1 or -1)
                        end
                    end
                end
            end
            return false
        end, Player.HistoryPhase)
        return hasCard and colorx == 0
    end,
    on_use = function(self, event, target, player, data)
        target.room:drawCards(target, 2, self.name, "bottom")
    end,
})

shoushilimin:addEffect(fk.AfterCardsMove,{
    can_refresh = function(self, event, target, player, data)
        if not player:hasSkill(self) then return false end
        return table.find(
                Fk:currentRoom().alive_players,
                function(p)
                    return p.phase == Player.Play
                end
        )
    end,
    on_refresh = function(self, event, target, player, data)
        local colorx = { 0, 0 }
        local room = player.room
        room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
            for _, move in ipairs(e.data) do
                if move.toArea == Card.DiscardPile then
                    for _, info in ipairs(move.moveInfo) do
                        local card = Fk:getCardById(info.cardId)
                        if room:getCardArea(info.cardId) == Card.DiscardPile then
                            colorx[card.color] = colorx[card.color] + 1
                        end
                    end
                end
            end
            return false
        end, Player.HistoryPhase)
        local tip
        if colorx[1] == colorx[2] then
            tip = colorx[1] == 0 and "∅" or "√"
        else
            tip = "×"
        end
        room:setPlayerMark(player, "@mcdh_shoushilimin_black-phase", colorx[1])
        room:setPlayerMark(player, "@mcdh_shoushilimin_red-phase", colorx[2])
        room:setPlayerMark(player, "@mcdh_shoushilimin-phase", tip)
    end
})

Fk:loadTranslationTable {
    ["mcdh_shoushilimin"] = "庸利",
    [":mcdh_shoushilimin"] = "『守实利民』<br>锁定技，当一名角色的出牌阶段结束时，若弃牌堆中有本阶段内进入的牌，且其中两种颜色的牌数相等，则该角色从牌堆底摸两张牌。",
    ["@mcdh_shoushilimin-phase"] = "守实利民",
    ["@mcdh_shoushilimin_black-phase"] = "黑色",
    ["@mcdh_shoushilimin_red-phase"] = "<font color='red'>红色</font>",
}

return shoushilimin